package com.hilog.paymentservice.controller;

import com.hilog.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付服务控制器
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {

    private static final Logger log = LoggerFactory.getLogger(PaymentController.class);

    /**
     * 测试接口
     */
    @GetMapping("/test")
    public String test() {
        return "支付服务启动成功！";
    }

    /**
     * 扣款接口
     * @param orderId 订单ID
     * @return 扣款结果
     */
    @GetMapping("/deduct/{orderId}")
    public Map<String, Object> deductPayment(@PathVariable("orderId") String orderId) {
        log.info("开始扣款，订单ID: {}", orderId);
        if (true) {
            log.error("支付出现异常，订单ID: {}", orderId);
            throw new RuntimeException("支付出现异常");
        }

        // 模拟扣款逻辑
        Map<String, Object> result = new HashMap<>();
        result.put("orderId", orderId);
        result.put("amount", 99.99);
        result.put("status", "扣款成功");
        result.put("code", 200);
        result.put("message", "支付扣款成功，订单ID: " + orderId);
        
        log.info("支付扣款成功，订单ID: {}", orderId);
        return result;
    }

    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public Result<String> health() {
        return Result.success("支付服务运行正常");
    }
} 